<?php
namespace app\index\controller;

use think\facade\View;
use app\admin\model\TopicCategory as TopicCategoryModel;
use app\admin\model\TopicContent as TopicContentModel;

class Index extends Base
{
    public function index($cid=0)
    {
        $content_model = new TopicContentModel();

        $categories = TopicCategoryModel::field('id, pid, name, url')->select();

        $categoryTree = obj_tree($categories);

        View::assign('categoryTree', $categoryTree);
        
        $field = "id,title,title_pic,summary,topic_url";

        if ($cid==0) {
            View::assign('lastTopic', $content_model->field($field)->order('create_time desc')->limit(3)->select());
            View::assign('topTopic', $content_model->field($field)->where('top', 2)->order('sort desc,create_time desc')->limit(3)->select());
        } else {
            $category = TopicCategoryModel::field('id, pid, name, url')->find($cid);
            View::assign('category', $category);
            
            $listTopic = $content_model->field($field)->where('cid', $cid)->select();
            View::assign('listTopic', $listTopic);
        }

        return View::fetch('index');
    }

    public function test()
    {
        return "test";
    }
}
